/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings;

import static com.google.android.tv.settings.TvSettings.System.SCREEN_DIM_TIMEOUT;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static android.provider.Settings.System.SOUND_EFFECTS_ENABLED;

import com.android.settings.R;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.Settings;
import android.preference.PreferenceScreen;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.List;
import android.util.Log;
import com.hisense.settings.util.Wrapper;

/**
 * Handles settings menu for cc displayoption settings.  
 *
 * @author huangjiufa
 */
public class CCDisplayOptionSettings extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener{
   
    private static final String TAG = "CCDisplayOptionSettings";

    private static final String KEY_DISPLAY_OPTION_TEXT = "cc_display_option_text";
	
    private static final String KEY_TEXT_CUSTOM_SETTINGS = "cc_text_custom_settings";
	

  	//As Broadcast 0
  	//Custom 1
	private static final int VALUE_DEFAULT_TEXT_MODE = 0;
	

	
    private ListPreference mCCDisplayOptionTextPreference;
	private PreferenceScreen mCCTextCustomSettingsPreference;

    private SharedPreferences mAppPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.cc_display_option_settings);

		mAppPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        mCCDisplayOptionTextPreference = (ListPreference) findPreference(KEY_DISPLAY_OPTION_TEXT);
		mCCTextCustomSettingsPreference = (PreferenceScreen)findPreference(KEY_TEXT_CUSTOM_SETTINGS);;

		int currentTextMode = getTextMode(VALUE_DEFAULT_TEXT_MODE);
		Log.d(TAG, "&&&&&&&&&&&&&  currentTextMode value: " + currentTextMode);
		if(getCharacterSize()==0){
			currentTextMode=0;
		}
        mCCDisplayOptionTextPreference.setValue((String.valueOf(currentTextMode)));
        mCCDisplayOptionTextPreference.setOnPreferenceChangeListener(this);
		updateTextModeSummary((String.valueOf(currentTextMode)));
		Log.d(TAG, "getCharacterSize()"+getCharacterSize());
		

		if (mCCDisplayOptionTextPreference.getValue().equals("0"))
		{
			Log.d(TAG, "&&&&mCCDisplayOptionTextPreference.getValue().equals(0)");
			mCCTextCustomSettingsPreference.setEnabled(false);
		}
		
    }



    private int getTextMode(int defaultValue) {
        int value = defaultValue;
        if(mCCDisplayOptionTextPreference.getValue()==null)
			setTextMode(0);
		Log.d(TAG, "getTextMode = "+mCCDisplayOptionTextPreference.getValue()+"***");
	    value =  Integer.parseInt(mCCDisplayOptionTextPreference.getValue());
		//value = Wrapper.getWrapperInstance().getTextMode();

        return value;
    }


    private void setTextMode(int defaultValue) {
        int value = defaultValue;
		if (value == 0)
		{
			setAllCustToBroadcast();
			mCCTextCustomSettingsPreference.setEnabled(false);
		}
		else
		{
			     try {

					recoveryAllCustValue();
					mCCTextCustomSettingsPreference.setEnabled(true);
					/*add begin by mali, 2013-03-19 , both of them are black ,then set character color is white, and set style4*/
					if(getCharacterColor() && getBackgroundColor())
					{
					      Log.d(TAG, "----both of them are black--- ");
					       setCharacterStyle(4);
						setCharacterColor(2);
						
					}
					/*add end by mali*/
	            } catch (NumberFormatException e) {
	                Log.e(TAG, "--ccdisplay could not persist screen timeout setting", e);
	            }
		
		}
		//Wrapper.getWrapperInstance().setTextMode(value);
		mCCDisplayOptionTextPreference.setValue((String.valueOf(value)));
		Log.d(TAG, "************8setTextMode : "+value);

    }
	
	public void resetcc(int Value){
		setAllCustToBroadcast();
	} 


private boolean getCharacterColor() 
{
	       int value = 0;
		value = Wrapper.getWrapperInstance().getTextColor();
		Log.d(TAG, "--------CCDisplayGetCharacterColor---value=: " + value);
		if (value == 1)
		{
			return true;
		
		}
	    	return false;
}

private boolean getBackgroundColor() 
{
       int value=0;
	value = Wrapper.getWrapperInstance().getBackgroundColor();
	Log.d(TAG, "--------CCDisplayGetBackgroundColor---value=: " + value);
	if (value == 1)
	{
		return true;
	}
      return false;
}
    private void updateTextModeSummary(Object value) {
        CharSequence[] summaries = getResources().getTextArray(R.array.cc_display_option_text_entries);
        CharSequence[] values = mCCDisplayOptionTextPreference.getEntryValues();
        for (int i=0; i<values.length; i++) {
            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
            //        + mAnimations.getValue());
            if (values[i].equals(value)) {
                mCCDisplayOptionTextPreference.setSummary(summaries[i]);
                break;
            }
        }
    }

    private static final String KEY_TEXT_CHARACTER_SIZE = "cc_text_character_size";
    private static final String KEY_TEXT_CHARACTER_STYLE = "cc_text_character_style";
    private static final String KEY_TEXT_CHARACTER_COLOR = "cc_text_character_color";
    private static final String KEY_TEXT_CHARACTER_OPACITY = "cc_text_character_opacity";
    private static final String KEY_TEXT_EDGE_TYPE = "cc_text_edge_type";
    private static final String KEY_TEXT_EDGE_COLOR = "cc_text_edge_color";
    private static final String KEY_TEXT_BACKGROUND_OPACITY = "cc_text_background_opacity";
    private static final String KEY_TEXT_BACKGROUND_COLOR = "cc_text_background_color";

    private void setAllCustToBroadcast() {
	    setCharacterSize(0);
	    setCharacterStyle(0);
	    setCharacterColor(0);
	    setCharacterOpacity(0);
	    setEdgeType(0);
	    setEdgeColor(0);
	    setBackgroundOpacity(0);
	    setBackgroundColor(0);
	};

    private void recoveryAllCustValue() {
		Log.d(TAG, "************recoveryAllCustValue ****** ");
		setCharacterSize(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_CHARACTER_SIZE, "0"))+1);
		setCharacterStyle(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_CHARACTER_STYLE, "0"))+1);
		//setCharacterColor(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_CHARACTER_COLOR, "0"))+1);
		setCharacterColor(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_CHARACTER_COLOR, "0"))+1);
		setCharacterOpacity(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_CHARACTER_OPACITY, "0"))+1);
		setEdgeType(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_EDGE_TYPE, "0"))+1);
		setEdgeColor(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_EDGE_COLOR, "0"))+1);
		setBackgroundOpacity(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_BACKGROUND_OPACITY, "0"))+1);
		setBackgroundColor(Integer.parseInt(mAppPreferences.getString(KEY_TEXT_BACKGROUND_COLOR, "0"))+1);
	}




    private void setCharacterSize(int defaultValue) {
        int value = defaultValue;
	  Log.d(TAG, "************setCharacterSize= ****** "+value);
		Wrapper.getWrapperInstance().setTextSize(value);
    }
	private int getCharacterSize() {       
		return Wrapper.getWrapperInstance().getTextSize();
    }

    private void setCharacterStyle(int defaultValue) {
        int value = defaultValue;
		  Log.d(TAG, "************setCharacterStyle= ****** "+value);
		Wrapper.getWrapperInstance().setTextStyle(value);
        

    }

    private void setCharacterOpacity(int defaultValue) {
        int value = defaultValue;
			  Log.d(TAG, "************setCharacterOpacity= ****** "+value);
		Wrapper.getWrapperInstance().setTextOpacity(value);

    }


    private void setCharacterColor(int defaultValue) {
        int value = defaultValue;
					  Log.d(TAG, "************setCharacterColor= ****** "+value);
		Wrapper.getWrapperInstance().setTextColor(value);

    }


    private void setEdgeType(int defaultValue) {
        int value = defaultValue;
		Wrapper.getWrapperInstance().setTextEdgeType(value);

    }


    private void setEdgeColor(int defaultValue) {
        int value = defaultValue;
			  Log.d(TAG, "************setEdgeColor= ****** "+value);
		Wrapper.getWrapperInstance().setTextEdgeColor(value);

    }


    private void setBackgroundOpacity(int defaultValue) {
        int value = defaultValue;
        
		Wrapper.getWrapperInstance().setBackgroundOpacity(value);

    }

    private void setBackgroundColor(int defaultValue) {
        int value = defaultValue;
        
		Wrapper.getWrapperInstance().setBackgroundColor(value);
    }


	
    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        return false;
    }

    public boolean onPreferenceChange(Preference preference, Object objValue) {
        final String key = preference.getKey();
        if (KEY_DISPLAY_OPTION_TEXT.equals(key)) {
            int value = Integer.parseInt((String) objValue);
            try {
				setTextMode(value);
				updateTextModeSummary(objValue);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }

        return true;
    }

}
